package com.jihu.sharding.algorithm;

import org.apache.shardingsphere.api.sharding.standard.RangeShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;

import java.util.Arrays;
import java.util.Collection;

/**
 * standard策略之自定义数据源范围算法类
 * <p>
 * 执行比如 select * from course where cid between ? and ?
 */
public class MyRangeDSShardingAlgorithm implements RangeShardingAlgorithm<Long> {

    /**
     * 数据源范围分片方法
     *
     * @param availableTargetNames 所有的真实数据源名称集合
     * @param shardingValue        过滤条件
     * @return 需要查询的数据源名称
     */
    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Long> shardingValue) {
        //select * from course where cid between 1 and 100;
        Long upperVal = shardingValue.getValueRange().upperEndpoint();//100
        Long lowerVal = shardingValue.getValueRange().lowerEndpoint();//1

        String logicTableName = shardingValue.getLogicTableName();
        // 不确定范围数据分布在哪些数据源中, 所以返回全部的数据源信息
        return Arrays.asList("m1", "m2");
    }
}
